'use strict'


Promise.all(_.map([
        'base', 'user', config.store.use_mock?'mock':'api'
    ].concat(config.store.extra_modules), (name) => {
    return httpLoader(config.store.path + name)
})).then((m) => {
    return new Vuex.Store({
        modules: m
    })
}).then((store) => {
    Promise.all([
            axios(store.getters.api.user_info),
            axios(store.getters.api.user_menu)
    ]).then(([info, menu]) => {
        store.commit('menu', menu.data)
        const router = new VueRouter({
            routes: store.getters.routes
        })
        _.assign(window, {$router: router, $store: store, $dialog: Vue.prototype.$dialog})
        store.api = {
            call: (api_desc, {message, params, form, noloading}, resolve) => {
                const api_info = store.getters.api[api_desc]
                if(!api_info) {
                    throw '未定义数据API:' + api_desc
                }
                if(!noloading)
                    $dialog.loading.open('处理中')
                const args = _.merge({data: form, params}, api_info)
                if(args.url.call) {
                    args.url = args.url()
                }
                axios(args).then((resp) => {
                    if(!noloading)
                        $dialog.loading.close()
                    resolve(resp)
                }).catch((ex) => {
                    if(!noloading)
                        $dialog.loading.close()
                    $dialog.notify({mes: message + ':' + ex})
                    if(config.store.use_mock) {
                        throw ex
                    }
                })
            }
        }
        router.beforeEach((to, from, next) => {
            if(store.getters.isLogin || to.path == '/') {
                if(to.meta && to.meta.protect == 'password' &&
                    !(new Date().getTime() < store.getters.grantLimit)) {
                    if(to.path != '/protect') {
                        store.commit('redirect', to.path)
                        next('/protect')
                    }
                } else {
                    store.commit('pushRoute', to.path)
                    document.title = to.meta.title
                    next()
                }
            } else {
                $dialog.notify({mes: '请绑定账号后使用！'})
                if(to.path != '/') {
                    store.commit('redirect', to.path)
                    next('/')
                }
            }
            if(to.meta.webar != store.getters.webar.show) {
                store.commit('webar', {show: to.webar, nav: undefined})
            } else {
                store.commit('webarNav', undefined)
            }
        })
        if(info.data) {
            store.commit('info', info.data)
        } else {
            throw '请通过微信公众号访问！'
        }

        const App = httpVueLoader('app.vue')

        _.each(config.components, (comp) => {
            Vue.component(comp.name, httpVueLoader(comp.path))
        })

        new Vue({
            el: '#app',
            router,
            store,
            template: '<App/>',
            components: {
                App
            }
        })
    }).catch(config.error)
}).catch(config.error)

parent = function(){}
document.write = function(){}
